﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using CWB.Common.Constant;
using CWB.Common.DTO;
using CWB.Common.Logging;
using CWB.Common.Utilities;
using CWB.Web.Configuration;
using CWB.Web.DB;
using CWB.Web.Models;
using CWB.Web.Models.Configuration;
using CWB.Web.MVC.BaseController;
using CWB.Web.MVC.Filters;
using CWB.WebSite.Models;
using CWB.WebSite.Models.Proeuct;

namespace CWB.WebSite.Controllers.Service
{
    [PermissionFilterAttvibute(loginType = LoginType.Bo)]
    public class ProductServiceController : MultiCultureBaseController
    {
        private static readonly ISysLog Log = SysLog.GetLogger(typeof(ProductServiceController));

        public ProductServiceController(LanguageInfo languageInfo)
            : base(languageInfo)
        {
            ControllerPermision = Permission.Private;
        }

        //
        // GET: /ProductService/

        //
        // GET: /BouService/
        [HttpPost]
        public JsonResult Create(string prodid, string prodName, string unit, string unitValue, string fee)
        {
            ResultBase result = new ResultBase();
            try
            {
                int _unit = int.Parse(unit);
                int _unitValue = int.Parse(unitValue);
                int _fee = int.Parse(fee);

                DbTableProduct o = new DbTableProduct();
                if (o.CheckIdIsExist(prodid))
                {
                    result.Message = Localization.TrandlateMessage(Err.ProductIdIsExisting);
                }
                else
                {
                    DbModuleProd dmp = new DbModuleProd()
                    {
                        fee = _fee,
                        prodid = prodid,
                        prodName = prodName,
                        unit = _unit,
                        unitvalue = _unitValue
                    };
                    if (o.createProdItem(dmp))
                    {
                        result.Message = Localization.TranslateMessage(Msg.CreateProduceSuccess);
                        result.Result = ReturnCode.Success;
                        Log.Debug("Create Product Item success, ProdId : {0}", prodid);
                    }
                    else
                    {
                        result.Message = Localization.TrandlateMessage(Err.SaveOrCreateDataGoProblem);
                        Log.Debug("Create Product Item failed, ProdId : {0}", prodid);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                result.setException(Err.ReceiveParameterTypeIncorrect, "Create");
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult Search(int? page, string searchkey)
        {
            int iPage = 1;
            string sSearchKey = "";
            if (page.HasValue)
            {
                iPage = int.Parse(page.Value.ToString());
            }
            if (!string.IsNullOrEmpty(searchkey))
            {
                sSearchKey = searchkey;
            }
            DbTableProduct odb = new DbTableProduct();
            Paging pg = new Paging()
            {
                pageSize = AppConfigManager.SystemSetting.UsersPageingSize,
                currentPage = iPage
            };
            DBSortingPara dbsp = new DBSortingPara();
            KeyValuePair<string, string> cond = new KeyValuePair<string, string>("prodid", sSearchKey);
            dbsp.AddSortingItem("prodid", SortingType.ASC);

            ProeuctListing dataModel = new ProeuctListing();
            try
            {
                List<DbModuleProd> listData = odb.getAllProdItem(ref pg, cond, dbsp);
                if (listData.Count > 0)
                {
                    dataModel.Result = ReturnCode.Success;
                    dataModel.listModel = listData;
                    dataModel.totalPages = pg.maxPage;
                }
                else
                {
                    dataModel.Message = Localization.TrandlateMessage(Msg.ThereAreNoDateFound);
                    dataModel.Result = ReturnCode.SuccessWithNoData;
                }
            }
            catch (Exception ex)
            {
                dataModel.setException(ex, "ProductService_Search");
            }
            return Json(dataModel, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult Delete(int sn)
        {
            DbTableProduct odb = new DbTableProduct();
            ResultBase result = new ResultBase();
            //check the user is exist.
            if (odb.DeleteProdItemBySn(sn))
            {
                result.Result = ReturnCode.Success;
                result.Message = Localization.TrandlateMessage(Msg.DeleteSuccess);
            }
            else
            {
                result.Message = Localization.TrandlateMessage(Err.SaveOrCreateDataGoProblem);
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }

        [HttpPost]
        public JsonResult Update(int sn, string prodName, string unit, string unitValue, string fee)
        {
            ResultBase result = new ResultBase();
            try
            {
                int _unit = int.Parse(unit);
                int _unitValue = int.Parse(unitValue);
                int _fee = int.Parse(fee);

                DbTableProduct o = new DbTableProduct();
                DbModuleProd dmp = new DbModuleProd()
                {
                    sn = sn,
                    fee = _fee,
                    prodName = prodName,
                    unit = _unit,
                    unitvalue = _unitValue
                };
                if (o.updateProdItemBySN(dmp))
                {
                    result.Message = Localization.TranslateMessage(Msg.UpdateProductSuccess);
                    result.Result = ReturnCode.Success;
                    Log.Debug("Update Product Item success, ProdSn : {0}", sn);
                }
                else
                {
                    result.Message = Localization.TrandlateMessage(Err.SaveOrCreateDataGoProblem);
                    Log.Debug("Update Product Item failed, ProdSn : {0}", sn);
                }
            }
            catch (Exception ex)
            {
                Log.Exception(ex);
                result.setException(Err.ReceiveParameterTypeIncorrect, "Update");
            }
            return Json(result, JsonRequestBehavior.DenyGet);
        }

    }
}
